//
//  UITextField+AccessoryView.m
//  Objective-C
//
//  Created by S_Line on 2018/8/21.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "UITextField+AccessoryView.h"

@implementation UITextField (AccessoryView)

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [self setInputAccessoryView];
    [super willMoveToSuperview:newSuperview];
}

- (void)setInputAccessoryView {
    CGFloat inputAccessoryViewH = 40;
    UIView *inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, inputAccessoryViewH)];
    inputAccessoryView.backgroundColor = [UIColor colorWithHexString:@"#f8f8f8"];
    UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
    separatorView.backgroundColor = [UIColor colorWithHexString:@"#ccd0d6"];
    [inputAccessoryView addSubview:separatorView];
    CGFloat finishButtonW = 40;
    CGFloat finishButtonX = kScreenWidth - finishButtonW - 25;
    UIButton *finishButton = [[UIButton alloc] initWithFrame:CGRectMake(finishButtonX, 0, finishButtonW, inputAccessoryViewH)];
    finishButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [finishButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [finishButton setTitle:@"测试" forState:UIControlStateNormal];
    [finishButton addTarget:self action:@selector(endEdit) forControlEvents:UIControlEventTouchUpInside];
    [inputAccessoryView addSubview:finishButton];
    
    self.inputAccessoryView = inputAccessoryView;
}

- (void)endEdit {
    [[UIApplication sharedApplication].keyWindow endEditing:true];
}

@end
